package one.bugu.android.demon.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.*;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import one.bugu.android.demon.R;
import one.bugu.android.demon.ui.refresh.util.DensityUtil;
import one.bugu.android.demon.util.ColorUtil;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Date: 2017/9/22
 * Time: 17:22
 * Description: show at Http requesting
 */
public class DialogDown extends Dialog {


    private Context mContext;
    private TextView titleTextView;
    private Button cancelBtn;
    private LinearLayout contentLayout;
    private OnDialogVersionListener onDialogVersionListener;
    private String title;
    private List<String> listContent;

    public DialogDown(Context context, String title, List<String> listContent) {
        super(context, com.juefeng.android.framework.R.style.DialogTheme);
        this.mContext = context;
        this.title = title;
        this.listContent = listContent;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_down, null);
        setContentView(view);
        titleTextView = view.findViewById(R.id.title);
        cancelBtn = view.findViewById(R.id.negativeButton);
        contentLayout = view.findViewById(R.id.layout_content);
        titleTextView.setText(title);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        WindowManager wm = this.getWindow().getWindowManager();
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        lp.width = width;
        dialogWindow.setAttributes(lp);

        if (listContent!=null&&listContent.size()>0){
            for (String s:listContent){
                TextView textView = new TextView(mContext);
                textView.setText(s);
                textView.setTextColor(ColorUtil.getColor(mContext,R.color.colorTextDefault));
                textView.setTextSize(16);
                textView.setGravity(Gravity.CENTER);
                textView.setPadding(0,DensityUtil.dp2px(5),0,DensityUtil.dp2px(5));
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onDialogVersionListener!=null){
                            onDialogVersionListener.onSure(((TextView)v).getText().toString());
                        }
                        dismiss();
                    }
                });
                contentLayout.addView(textView,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            }
        }

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public OnDialogVersionListener getOnDialogVersionListener() {
        return onDialogVersionListener;
    }

    public void setOnDialogVersionListener(OnDialogVersionListener onDialogVersionListener) {
        this.onDialogVersionListener = onDialogVersionListener;
    }

    public interface OnDialogVersionListener {
        void onSure(String string);
    }



}
